import md5 from 'js-md5';
import {Message} from 'element-ui';
import types from '../types.js';
import {get, post, put} from '../../utils/serviceHelper.js';
import storageHelper from '../../utils/storageHelper';
import Vue from 'vue'

const state = {
    userInfo: null,
    loginToken: ''
}

const getters = {
    loginToken(state) {
        if (state.loginToken) {
            return state.loginToken;
        } else {
            return storageHelper.get('efws_manage_token');
        }
    },
    userInfo(state) {
        if (state.userInfo) {
            return state.userInfo;
        } else {
            return storageHelper.get('manage_userInfo');
        }
    }
}

const actions = {
    getRole({commit, state}, param) {

        return new Promise((resolve, reject) => {
            get('/man/role', param).then((res) => {
                if (res.code == 0) {
                    resolve(res);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    getKey({commit, state}, param) {

        return new Promise((resolve, reject) => {
            get('/code/key', param).then((res) => {
                if (res.code == 0) {
                    resolve(res);
                } else {
                    reject(res.messages);
                }
            })
        });
    },
    loginQuit({commit, state}) {
        commit(types.USER_INFO, null);

        storageHelper.remove('efws_manage_token');
        storageHelper.remove('manage_userInfo');
        storageHelper.clear();
    },
    loginSen({commit, state, dispatch}, param) {
        return new Promise((resolve, reject) => {
            post('/main/login', param).then((res) => {

                if (res.code == 0) {
                    commit(types.USER_INFO, res.rs.user);
                    commit(types.LOGIN_TOKEN, res.rs.token);
                    storageHelper.set('efws_manage_token', res.rs.token);
                    storageHelper.set('manage_userInfo', res.rs.user);
                    resolve(res);
                } else {
                    resolve(res);
                }
            })
        });
    },
    forgetPasswordReq({commit, state}, param) {

        return new Promise((resolve, reject) => {
            get('/manage/main/smsPassword', param).then((res) => {
                if (res) {
                    resolve(res.rs.result);
                }
            })

        });
    },

    updateAccount({commit, state}, param) {

        return new Promise((resolve, reject) => {
            put('/account', param).then((res) => {
                if (res.code == 0) {
                    commit(types.USER_INFO, res.rs);
                    storageHelper.set('manage_userInfo', res.rs);
                    resolve(res);
                } else {
                    reject(res);
                }
            })
        });
    },
    updatePassword({commit, state}, param) {
        return new Promise((resolve, reject) => {
            put('/account/updatePassword', param).then((res) => {
                if (res.code == 0) {
                    resolve(res);
                } else {
                    reject(res);
                }
            })
        });
    }
}
const mutations = {
    [types.USER_INFO](state, data) {
        // state.userInfo = data;
        Vue.set(state, 'userInfo', data)
    },
    [types.LOGIN_TOKEN](state, data) {
        state.loginToken = data;
    }


}

export default {
    state,
    getters,
    actions,
    mutations
}
